package com.weme.live.fragment

import android.animation.ValueAnimator
import android.content.Context
import android.view.inputmethod.InputMethodManager
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.activityViewModels
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.gyf.immersionbar.OnKeyboardListener
import com.weme.common.base.BaseFragment
import com.weme.common.util.setInvisible
import com.weme.common.util.setVisible
import com.weme.live.activity.LiveRoomActivity
import com.weme.live.databinding.FragmentLiveRoomBinding
import com.weme.live.viewmode.LiveViewMode


class LiveRoomFragment : BaseFragment<FragmentLiveRoomBinding, LiveViewMode>() {

    private val mViewModel: LiveViewMode by activityViewModels()
    private var countdownAnimator: ValueAnimator? = null
    override fun startOperate() {
        ImmersionBar.with(this).hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR).keyboardEnable(false)
//            .setOnKeyboardListener(object : OnKeyboardListener {
//                override fun onKeyboardChange(isPopup: Boolean, keyboardHeight: Int) {
//                    mBinding.liveChat.updateLayoutParams<ConstraintLayout.LayoutParams> {
//                        bottomMargin = keyboardHeight
//                    }
//                }
//
//            })
            .titleBar(mBinding.viewLiveTop).init()
        mBinding.viewLiveTop.setViewMode(requireActivity())
        mBinding.liveChat.setViewMode(requireActivity())

        mViewModel.gestureGuideFinishedLiveData.observe(this) { //      mBinding.imGuidance.gone()
        }

        //这个是是否软键盘 和是否清屏的it 不一样
        mViewModel.showKeyboardLiveData.observe(this) {
            mBinding.liveList.setVisible(it)
            mBinding.liveChat.getImOverturn().setVisible(it)

        } //是否清屏
        mViewModel.screenClearLiveData.observe(this) {
            mBinding.liveList.setInvisible(!it)

            mBinding.liveChat.setInvisible(!it)
            mBinding.viewLiveTop.setScreenClear(it)
        }

    }

    override fun clickListener() {
        mBinding.calView.setOnClickListener {
            if ((requireActivity() as LiveRoomActivity).isKeyboardVisible) {
                val inputMethodManager = requireActivity().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                inputMethodManager.hideSoftInputFromWindow(mBinding.calView.windowToken, 0)
            } else {
                (requireActivity() as LiveRoomActivity).navigateToPage()
            }

        }
    }

    override fun onDestroy() {
        super.onDestroy()
        countdownAnimator?.cancel()
        countdownAnimator?.clone()
        countdownAnimator = null
        mBinding.viewLiveTop.onDestroy()
    }

    override fun getViewModel(): Class<LiveViewMode> = LiveViewMode::class.java
}